package org.jeecg.cn.edu.scau.vm.machineTime.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.cn.edu.scau.vm.machineTime.entity.VmGroupSettings;
import org.jeecg.cn.edu.scau.vm.machineTime.service.IVmGroupSettingsService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Api(tags = "分组配额相关配置")
@RestController
@RequestMapping("/vm/vmGroupSettings")
@Slf4j
public class VmGroupSettingsController {
    @Resource
    private IVmGroupSettingsService vmGroupSettingsService;

    /**
     * 分页列表查询
     *
     * @param vmGroupSettings
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "vm_group_settings-分页列表查询")
    @ApiOperation(value = "vm_group_settings-分页列表查询", notes = "vm_group_settings-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<VmGroupSettings>> queryPageList(VmGroupSettings vmGroupSettings,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<VmGroupSettings> queryWrapper = QueryGenerator.initQueryWrapper(vmGroupSettings, req.getParameterMap());
        Page<VmGroupSettings> page = new Page<VmGroupSettings>(pageNo, pageSize);
        IPage<VmGroupSettings> pageList = vmGroupSettingsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param vmGroupSettings
     * @return
     */
    @AutoLog(value = "vm_group_settings-添加")
    @ApiOperation(value = "vm_group_settings-添加", notes = "vm_group_settings-添加")
    @RequiresPermissions("vm:vm_group_settings:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody VmGroupSettings vmGroupSettings) {
        vmGroupSettingsService.save(vmGroupSettings);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param vmGroupSettings
     * @return
     */
    @AutoLog(value = "vm_group_settings-编辑")
    @ApiOperation(value = "vm_group_settings-编辑", notes = "vm_group_settings-编辑")
    @RequiresPermissions("vm:vm_group_settings:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody VmGroupSettings vmGroupSettings) {
        vmGroupSettingsService.updateById(vmGroupSettings);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "vm_group_settings-通过id删除")
    @ApiOperation(value = "vm_group_settings-通过id删除", notes = "vm_group_settings-通过id删除")
    @RequiresPermissions("vm:vm_group_settings:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        vmGroupSettingsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "vm_group_settings-批量删除")
    @ApiOperation(value = "vm_group_settings-批量删除", notes = "vm_group_settings-批量删除")
    @RequiresPermissions("vm:vm_group_settings:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.vmGroupSettingsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "vm_group_settings-通过id查询")
    @ApiOperation(value = "vm_group_settings-通过id查询", notes = "vm_group_settings-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<VmGroupSettings> queryById(@RequestParam(name = "id", required = true) String id) {
        VmGroupSettings vmGroupSettings = vmGroupSettingsService.getById(id);
        if (vmGroupSettings == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(vmGroupSettings);
    }
    
}
